home *** CD-ROM | disk | FTP | other *** search
/ Transactor / Transactor_23_1988_Transactor_Publishing.d64 / brk trap.cbm < prev    next >
Text File  |  2023-02-26  |  3KB  |  157 lines

  1. .opt nosym
  2. ;put"@:s/brk"
  3. ;********************************
  4. ;*                              *
  5. ;*  simulating a trap with brk  *
  6. ;*  --------------------------  *
  7. ;*                              *
  8. ;*                              *
  9. ;* brk vector is diverted so    *
  10. ;* that "invisible" subroutines *
  11. ;* can be called.               *
  12. ;*                              *
  13. ;*                              *
  14. ;*  - by tom hughes  v022287 -  *
  15. ;*                              *
  16. ;********************************
  17. .skip
  18. ;
  19. ;c64 equates
  20. ;
  21. cbinv = $0316 ;brk vector (2)
  22. chrout = $ffd2 ;output a char
  23. clrchn = $ffcc ;i/o to defaults
  24. getin = $ffe4 ;input a char
  25. memory = $8d ;temp storage (2)
  26. oldbrk = $8b ;storage for standard brk (2)
  27. stack = $0100 ;65xx stack location
  28. tidyup = $febc ;recover from interrupt
  29. .skip
  30. * = $8000 ;sys 32768
  31. .skip
  32. ;------------------------------
  33. ;set brk vector to our routine
  34. ;------------------------------
  35. .skip
  36. ;in actual use, this would be a subroutine
  37. ;called once to divert the brk vector.
  38. .skip
  39.  jsr clrchn
  40.  sei ;disable interrupts
  41.  ldx cbinv
  42.  ldy cbinv+1
  43.  stx oldbrk ;save the old brk vector
  44.  sty oldbrk+1
  45.  ldx #<newbrk ;then set new vector
  46.  ldy #>newbrk
  47.  stx cbinv
  48.  sty cbinv+1
  49.  cli ;enable interrupts
  50. .page
  51. ;------------------------------
  52. ;demo brk handler
  53. ;------------------------------
  54. .skip
  55. ;this is just an example of how you
  56. ;would use brk from within a program.
  57. .skip
  58. demo5 ldy #0
  59. demo10 lda prompt,y ;print "number?"
  60.  beq demo20
  61.  jsr chrout
  62.  iny
  63.  bne demo10
  64. demo20 jsr getin ;check the keyboard
  65.  cmp #3 ;(if stop key, quit)
  66.  beq quit
  67.  cmp #'1         ;for numbers 1 thru 3
  68.  bcc demo20
  69.  cmp #'4
  70.  bcs demo20
  71.  jsr chrout
  72.  and #%00001111 ;make # hex 1 - 3
  73.  sta trpnm ;save in our own program
  74.  lda #13 ;print a carriage return
  75.  jsr chrout
  76.  brk ;execute trap
  77. trpnm .byt 0 ;(trap #)
  78.  jmp demo5 ;after brk, prg continues here
  79. .skip
  80. quit sei
  81.  ldx oldbrk
  82.  ldy oldbrk+1
  83.  stx cbinv
  84.  sty cbinv+1
  85.  cli
  86.  rts ;back to basic
  87. .skip
  88. prompt .byt 13,13,'number (1 - 3)? ',0
  89. .page
  90. ;==============================
  91. ;new brk routine
  92. ;==============================
  93. .skip
  94. ;entry: (1) interrupts disabled (except nmi)
  95. ;           so jiffy clock is off.
  96. ;
  97. ;       (2) on entry stack looks like this...
  98. ;           (assuming old sp was at $f6)
  99. ;
  100. ;           $01f6      <- old sp
  101. ;           $01f5  pch     (stack+6)
  102. ;           $01f4  pcl     (stack+5)
  103. ;           $01f3   sr     (stack+4)
  104. ;           $01f2   .a     (stack+3)
  105. ;           $01f1   .x     (stack+2)
  106. ;           $01f0   .y     (stack+1)
  107. ;           $01ef      <- current sp
  108. ;
  109. ;       (3) expects trap # after brk instr.
  110. ;           (this location can be found by
  111. ;           using the pc saved on stack -1.)
  112. ;
  113. newbrk tsx ;get current sp to .x
  114.  lda stack+6,x ;to find pc-high
  115.  sta memory+1
  116.  lda stack+5,x ;and pc-low on the stack
  117.  sta memory ;save this address
  118.  bne new10 ;and subtract -1 from it
  119.  dec memory+1 ;so we can locate trap #
  120. new10 dec memory
  121.  ldy #0
  122.  lda (memory),y ;get trap #
  123.  tay ;adjust it so 1-3
  124.  dey ;is now 0-2
  125.  tya
  126.  asl a ;multiply this # by 2
  127.  tay
  128.  lda table,y ;and use it to look up
  129.  sta memory ;trap addresses
  130.  iny
  131.  lda table,y
  132.  sta memory+1
  133.  jmp (memory) ;go to a trap routine
  134. .skip
  135. ;trap addresses
  136. ;
  137. table .word trap1
  138.  .word trap2
  139.  .word trap3
  140. .page
  141. ;//////////////////////////////
  142. ;demo trap routines
  143. ;//////////////////////////////
  144. .skip
  145. trap1 lda #'1
  146.  jsr chrout
  147.  jmp tidyup ;must end with this
  148. .skip
  149. trap2 lda #'2
  150.  jsr chrout
  151.  jmp tidyup
  152. .skip
  153. trap3 lda #'3
  154.  jsr chrout
  155.  jmp tidyup
  156. .end
  157.